package com.example.controller.user;

import com.example.common.Result;
import com.example.entity.dto.FarmerDTO;
import com.example.entity.item.Items;
import com.example.entity.user.Farmer;
import com.example.entity.user.Room;
import com.example.service.FarmerService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/farmer")
public class FarmerController {

    @Resource
    private FarmerService farmerService;

    @DeleteMapping("/delete/{id}")
    public Result deleteFarmerById(@PathVariable Integer id)
    {
        farmerService.deleteFarmerById(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateFarmerById(@RequestBody Farmer farmer)
    {
        farmerService.updateFarmerById(farmer);
        return Result.success();
    }

    @GetMapping("/selectFarmerById/{id}")
    public Result selectFarmerById(@PathVariable Integer id)
    {
        Farmer farmer=farmerService.selectFarmerById(id);
        return Result.success(farmer);
    }

    @GetMapping("/selectFarmerByUsername")
    public Result selectFarmerByUsername(String username)
    {
        FarmerDTO farmerDTO=farmerService.selectFarmerByUsername(username);
        return Result.success(farmerDTO);
    }

    @GetMapping("/selectFarmerAll")
    public Result selectFarmerAll(Farmer farmer)
    {
        List<Farmer> list=farmerService.selectFarmerAll(farmer);
        return Result.success(list);
    }

    @GetMapping("/selectFarmerPage")
    public  Result selectFarmerPage(Farmer farmer,
                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue="10") Integer pageSize){
        PageInfo<Farmer> page=farmerService.selectFarmerPage(farmer,pageNum,pageSize);
        return Result.success(page);
    }

    @GetMapping("/selectItemPage/{itemOwner}")
    public Result selectItemPage(@PathVariable String itemOwner,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize)
    {
        PageInfo<Items> page=farmerService.selectItemPage(itemOwner,pageNum,pageSize);
      return Result.success(page);
    }

    @GetMapping("/selectRoomById/{roomId}")
    public Result selectRoomById(@PathVariable String roomId)
    {
        Room room=farmerService.selectRoomById(roomId);
        return Result.success(room);
    }



}
